<template>
  <div class="fh-table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="dataList"
      border
      fit
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelect"
        type="selection"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column
        v-if="showIndex"
        type="index"
        align="center"
        label="序号"
        width="60"
      ></el-table-column>
      <template v-for="item in propList" :key="item.prop">
        <el-table-column v-bind="item" align="center" show-overflow-tooltip>
          <!-- 替换默认插槽,接收传值并展示 -->
          <template #default="xxx">
            <!-- 通过slot展示,可以进行替换   :row将值传出,在替换时使用 -->
            <slot :name="item.prop" :row="xxx.row">
              {{ xxx.row[item.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          :currentPage="page.currentPage"
          :page-sizes="[10, 20, 30]"
          :page-size="page.pageSize"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    title: {
      type: String,
      default: ''
    },
    dataList: {
      type: Array,
      required: true
    },
    dataCount: {
      type: Number,
      default: 0
    },
    propList: {
      type: Array as any,
      required: true
    },
    showIndex: {
      type: Boolean,
      default: false
    },
    showSelect: {
      type: Boolean,
      default: false
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10 })
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  emits: ['SelectionChange', 'update:page'],
  setup(props, { emit }) {
    // table选中的值进行发射
    const handleSelectionChange = (value: any[]) => {
      emit('SelectionChange', value)
    }

    // 每页显示条目个数
    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }
    // 当前页数
    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }
    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped lang="less">
.fh-table {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
  background-color: #fff;

  .header {
    display: flex;
    height: 45px;
    padding: 0 5px;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 20px;
      font-weight: 700;
    }
    .handler {
      display: flex;
      align-items: center;
    }
  }
  .footer {
    display: flex;
    align-items: center;
    margin-top: 20px;
    display: flex;
    // 在flex布局中,没有 start end
    // 格式为: flex-start, flex-end
    justify-content: flex-end;
  }
}
</style>
